<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Facades\Auth;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    protected $user;

    public function __construct()
    {
        $this->middleware(function ($request, $next) {
            // 当前登陆账号
            $this->user = Auth::user();

            // 额外不检测的路由权限节点
            $arr = ['home', 'main'];
            // 当前访问路由
            $route_name = \Route::currentRouteName();
            // 判断是否有权限操作当前访问路由
            if (in_array($route_name, $arr) || $this->user->hasRole('超级管理员') || $this->user->can($route_name)) {
                return $next($request);
            } else {
                session()->flash('danger', '没有权限,请不要操作');
                return redirect()->back();
            }
        });
    }
}
